package com.zz.wsq.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zz.wsq.model.User;
import com.zz.wsq.service.UserService;

@RestController
public class ShowController {
	
	@Autowired
	UserService userService;
	
	@RequestMapping("/AllUser")
	public Map showAll(){
		Map m=new HashMap();
	List<User> ls=	userService.selectAll();
	m.put("ls", ls);
		return m;
	}
	@RequestMapping("/informationid")
	public Map selectOne(@RequestParam("id1") int id){
		Map m=new HashMap();
		User  u=userService.selectByPrimaryKey(id);
		m.put("u", u);
		return m;
	}
	@RequestMapping("/insert")
	public Map addUser(@RequestParam("name") String name,@RequestParam("pwd")String pwd,@RequestParam("email")String email){
		Map m=new HashMap();
		String result="N";
		User u=new User();
		u.setName(name);
		u.setPwd(pwd);
		u.setEmail(email);
		if(userService.selectByName(name).size()==0){
			userService.insertSelective(u);
			result="Y";
		}else{
			 result="N";
		}
		m.put("result", result);
		return m;
	}
	@RequestMapping("/update")
	public Map updateUser(@RequestParam("id") Integer id,@RequestParam("name") String name,@RequestParam("pwd")String pwd,@RequestParam("email")String email){
		Map m=new HashMap();
		String result="N";
		User u=new User();
		u.setId(id);
		u.setName(name);
		u.setPwd(pwd);
		u.setEmail(email);
		if(userService.selectByName(name).size()==0){
			
			userService.updateByPrimaryKeySelective(u);
			result="Y";
		}else{
			 result="N";
		}
		m.put("result", result);
		return m;
	}
	@RequestMapping("/delete")
	public Map deleteUser(@RequestParam("id" )Integer id){
		Map m=new HashMap();
		userService.deleteByPrimaryKey(id);
		return m;
		
	}

}
